﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using System.Windows.Forms;

namespace LGE
{
    public static class Engine
    {
        public static Game game;
        public static Viewport viewport { get { return game.GraphicsDevice.Viewport; } }

        public static void Init(Game g, GameScreen startScreen)
        {
            game = g;

            Input.Start();
            GameScreenManager.Start(startScreen);
        }

        public static void InitTileSystem(int blockWidth, int blockHeith, int tileSize, String tilesFolder)
        {
            Block.Initialize(blockWidth, blockHeith);
            Tile.Initialize(tileSize, tilesFolder);
        }

        public static bool InitGraphicsMode(GraphicsDeviceManager graphics, int iWidth, int iHeight, bool bFullScreen)
        {
            // If we aren't using a full screen mode, the height and width of the window can
            // be set to anything equal to or smaller than the actual screen size.
            if (bFullScreen == false)
            {
                if ((iWidth <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width)
                    && (iHeight <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height))
                {
                    graphics.PreferredBackBufferWidth = iWidth;
                    graphics.PreferredBackBufferHeight = iHeight;
                    graphics.IsFullScreen = bFullScreen;
                    graphics.ApplyChanges();
                    return true;
                }
            }
            else
            {
                // If we are using full screen mode, we should check to make sure that the display
                // adapter can handle the video mode we are trying to set.  To do this, we will
                // iterate thorugh the display modes supported by the adapter and check them against
                // the mode we want to set.
                foreach (DisplayMode dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
                {
                    // Check the width and height of each mode against the passed values
                    if ((dm.Width == iWidth) && (dm.Height == iHeight))
                    {
                        // The mode is supported, so set the buffer formats, apply changes and return
                        graphics.PreferredBackBufferWidth = iWidth;
                        graphics.PreferredBackBufferHeight = iHeight;
                        graphics.IsFullScreen = bFullScreen;
                        graphics.ApplyChanges();
                        return true;
                    }
                }
            }
            return false;
        }

        public static T Load<T>(String asset)
        {
            return game.Content.Load<T>(asset);
        }

        public static void MaximizeWindow()
        {
            Form form = (Form)Form.FromHandle(game.Window.Handle);
            form.WindowState = FormWindowState.Maximized;
        }
    }
}
